package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: UniquePiggy
 * @Date: 2020/11/22/10:12
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     *分页查询
     */
    @RequestMapping(value = "/findPage", method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            return permissionService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *查询所有权限
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        try {
            List<Permission> permissionList = permissionService.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);

        }
    }

    /**
     *新增权限
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Result add(@RequestBody Permission permission) {

        try {
            //先查询是否存在对应的权限名和权限关键字
            int count = permissionService.queryCount(permission);
            if (count > 0) {
                //如果存在，就直接返回错误信息
                return new Result(false, MessageConstant.ADD_PERMISSION_FAIL
                );
            }
            //不存在再添加权限信息
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }

    /**
     * 删除权限
     */
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public Result delete( @RequestParam (value = "id") Integer cmcc) {

        try {
            //先查询权限与角色是否有关联
            int count = permissionService.connCounts(cmcc);
            if (count > 0) {
                //如果关联，就直接返回错误信息
                return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
            }
            //没有关联再删除权限
            permissionService.delete(cmcc);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }

    /**
     * 根据权限查询检查结果,回显数据
     */
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(Integer id) {
        try {
           Permission permission = permissionService.findById(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


    /**
     * 编辑权限
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result update(@RequestBody Permission permission) {

        try {
            //先查询是否存在对应的权限名和权限关键字
            int count = permissionService.queryCount(permission);
            if (count > 0) {
                //如果存在，就直接返回错误信息
                return new Result(false, MessageConstant.UPDATE_PERMISSION_FAIL);
            }
            //不存在再添加权限信息
            permissionService.update(permission);
            return new Result(true, MessageConstant.UPDATE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }

}